﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 地图管理器
/// </summary>
public class MapManager : MonoBehaviour {
    //预制
    private GameObject m_prefab_tile;
    private GameObject m_prefab_wall;
    private GameObject m_prefab_spikes;
    private GameObject m_prefab_sky_spikes;
    private GameObject m_prefab_gem;

    //自身Transform
    private Transform m_transform;

    //玩家
    private PlayerController m_playerController;

    //地图列表
    public List<GameObject[]> mapList = new List<GameObject[]>();

    //方块对角线长度
    private float bottomLength = Mathf.Sqrt(2) * 0.254f;

    //掉落的位置
    private int index = 0;

    //概率
    private int pr_hole = 0;
    private int pr_spikes = 0;
    private int pr_sky_spikes = 0;
    private int pr_gem = 0;

    //颜色
    private Color color1 = new Color(124 / 255f, 155 / 255f, 230 / 255f);
    private Color color2 = new Color(125 / 255f, 169 / 255f, 233 / 255f);
    private Color color3 = new Color(87 / 255f, 93 / 255f, 169 / 255f);

    void Awake () {
        //预制加载
        m_prefab_tile = Resources.Load("tile_white") as GameObject;
        m_prefab_wall = Resources.Load("wall2") as GameObject;
        m_prefab_spikes = Resources.Load("moving_spikes") as GameObject;
        m_prefab_sky_spikes = Resources.Load("smashing_spikes") as GameObject;
        m_prefab_gem = Resources.Load("gem 2") as GameObject;

        //组件加载
        m_transform = gameObject.GetComponent<Transform>();
        m_playerController = GameObject.Find("Player").GetComponent<PlayerController>();

        //创造初始地图
        CreateMapItem(0f);
    }

    /// <summary>
    /// 创建地图元素
    /// </summary>
    public void CreateMapItem(float offsetZ)
    {
        
        for(int i = 0; i < 12; i++)
        {
            //单排
            GameObject[] item1 = new GameObject[6];
            for(int j = 0; j < 6; j++)
            {
                Vector3 pos = new Vector3(j * bottomLength, 0, (i + offsetZ) * bottomLength);
                Vector3 rot = new Vector3(-90, 45, 0);
                GameObject tile = null;
                //生成墙
                if (j == 0 || j == 5)
                {
                    tile = GameObject.Instantiate(m_prefab_wall, pos, Quaternion.Euler(rot)) as GameObject;
                    tile.GetComponent<MeshRenderer>().material.color = color3;
                }
                //地面
                else
                {
                    int pr = CalcPR();
                    if (pr == 0)
                    {
                        tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;
                        tile.GetComponent<MeshRenderer>().material.color = color1;
                        tile.GetComponent<Transform>().Find("normal_a2").GetComponent<MeshRenderer>().material.color = color1;
                        int prGem = CalcGemPR();
                        if (prGem == 1)
                        {
                            GameObject gem = GameObject.Instantiate(m_prefab_gem, tile.GetComponent<Transform>().position + Vector3.up * 0.06f,Quaternion.identity) as GameObject;
                            gem.GetComponent<Transform>().SetParent(tile.transform);
                        }
                    }else if (pr ==1)
                    {
                        tile = new GameObject();
                        tile.GetComponent<Transform>().position = pos;
                        tile.GetComponent<Transform>().rotation = Quaternion.Euler(rot);
                    }else if (pr == 2)
                    {
                        tile = GameObject.Instantiate(m_prefab_spikes, pos, Quaternion.Euler(rot)) as GameObject;
                    }else if (pr == 3)
                    {
                        tile = GameObject.Instantiate(m_prefab_sky_spikes, pos, Quaternion.Euler(rot)) as GameObject;
                    }
                }
                tile.transform.SetParent(m_transform);
                item1[j] = tile;
            }
            mapList.Add(item1);
            //End 单排

            //双排
            GameObject[] item2 = new GameObject[5];
            for (int j = 0; j < 5; j++)
            {
                Vector3 pos = new Vector3(j * bottomLength + bottomLength / 2, 0, (i + offsetZ) * bottomLength + bottomLength / 2);
                Vector3 rot = new Vector3(-90, 45, 0);

                GameObject tile = null;

                int pr = CalcPR();
                if (pr == 0)
                {
                    tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;
                    tile.GetComponent<MeshRenderer>().material.color = color2;
                    tile.GetComponent<Transform>().Find("normal_a2").GetComponent<MeshRenderer>().material.color = color2;
                    int prGem = CalcGemPR();
                    if (prGem == 1)
                    {
                        GameObject gem = GameObject.Instantiate(m_prefab_gem, tile.GetComponent<Transform>().position + Vector3.up * 0.06f, Quaternion.identity) as GameObject;
                        gem.GetComponent<Transform>().SetParent(tile.transform);
                    }
                }
                else if (pr == 1)
                {
                    tile = new GameObject();
                    tile.GetComponent<Transform>().position = pos;
                    tile.GetComponent<Transform>().rotation = Quaternion.Euler(rot);
                }
                else if (pr == 2)
                {
                    tile = GameObject.Instantiate(m_prefab_spikes, pos, Quaternion.Euler(rot)) as GameObject;
                }
                else if (pr == 3)
                {
                    tile = GameObject.Instantiate(m_prefab_sky_spikes, pos, Quaternion.Euler(rot)) as GameObject;
                }

                tile.transform.SetParent(m_transform);

                item2[j] = tile;
            }
            mapList.Add(item2);
            //End 双排
        }

        if (pr_gem == 0)
        {
            pr_gem = 2;
        }
    }

    public void StartTileDown()
    {
        StartCoroutine("TileDown");
    }

    public void StopTileDown()
    {
        StopCoroutine("TileDown");
    }

    /// <summary>
    /// 地面塌陷
    /// </summary>
    /// <returns></returns>
    private IEnumerator TileDown()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.2f);
            for(int i = 0; i < mapList[index].Length; i++)
            {
                Rigidbody rb = mapList[index][i].AddComponent<Rigidbody>();
                rb.angularVelocity = new Vector3(Random.Range(1,10), Random.Range(1, 10), Random.Range(1, 10));
                GameObject.Destroy(mapList[index][i], 1.0f);
            }
            if(m_playerController.z == index)
            {
                m_playerController.gameObject.AddComponent<Rigidbody>();
                m_playerController.GameOver();
                StopTileDown();
            }
            index++;
        }
    }

    /// <summary>
    /// 计算概率
    /// 0：瓷砖
    /// 1：坑洞
    /// 2：地面陷阱
    /// 3：天空陷阱
    /// </summary>
    /// <returns></returns>
    private int CalcPR()
    {
        int pr = Random.Range(0, 100);
        if (pr < pr_hole)
        {
            return 1;
        }else if (31 < pr && pr < pr_spikes + 30)
        {
            return 2;
        }else if(61<pr&& pr < pr_sky_spikes + 60)
        {
            return 3;
        }
        return 0;
    }

    private int CalcGemPR()
    {
        int pr = Random.Range(0, 100);
        if (pr < pr_gem)
        {
            return 1;
        }
        return 0;
    }

    public void AddPR()
    {
        pr_hole += 2;
        pr_spikes += 1;
        pr_sky_spikes += 1;
    }

    public void ResetMap()
    {
        Transform[] sonTransform = gameObject.GetComponentsInChildren<Transform>();
        for(int i = 1; i < sonTransform.Length; i++)
        {
            GameObject.Destroy(sonTransform[i].gameObject);
        }
        mapList.Clear();
        index = 0;
        pr_hole = 0;
        pr_spikes = 0;
        pr_sky_spikes = 0;
        pr_gem = 0;
        CreateMapItem(0f);
    }
}
